﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Data;
using System.Text;
using System.Data;


namespace Presentation
{
    public partial class TurmaCadastro : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnCadastrar_Click(object sender, EventArgs e)
        {
            //try
            //{

                TBTurma turma = new TBTurma();
                turma.Nome = TxtNome.Text;
               // turma.Semestre = Semestre.Text;
                turma.Semestre = Convert.ToInt32(TxtSemestre.Text);
                turma.Ano = Convert.ToInt32(TxtAno.Text);
                turma.IDGrade = Convert.ToInt32(ddlGrade.SelectedValue);

                using (DBUCNEntities banco = new DBUCNEntities())
                {
                    banco.AddToTBTurmas(turma);
                    banco.SaveChanges();
                }

                //        Response.Redirect("TurmaCadastro.aspx");

                //    }
                //    catch (Exception)
                     //{

                      //  throw;
                   // } 
                //}


                //protected void BntCadastrar_Click(object sender, System.EventArgs e);
                //{
                //return default(void);
                // }

                Response.Redirect("Default.aspx");
        }

        protected void ddlCurso_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int idCurso = Convert.ToInt32(ddlCurso.SelectedValue);

                //Abrindo a conexão com o banco
                using (DBUCNEntities banco = new DBUCNEntities())
                {
                    //FORMA 1:
                    //Realizar o select no banco
                    List<TBGrade> lista = banco.TBGrades.Where(g => g.IDCurso == idCurso).ToList();
                    //SELECT * FROM TBGRADE  G WHERE G.IDCURSO = @IDCURSO

                    //Inserindo o resultado na dropdown
                    ddlGrade.DataSource = lista;

                    //FORMA 2:
                    //from ALIAS in TABELA
                    //where
                    //select
                    var query = from tabela in banco.TBGrades
                                where tabela.IDCurso == idCurso
                                select tabela;


                    ddlGrade.DataSource = query;
                    ddlGrade.DataBind();
                }
            }
            catch (Exception)
            {
                Util.UtilCommom.exibirMensagem(this, "erro");
            }

         
        }
    }
}
